package home;


import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONException;

import ajaxmethods.SessionMethod;

import server.Initialize;
import server.db.StmtHelper;
import server.objects.Photo;

public class Home extends SessionMethod {

	@Override
	public void perform(HttpServletRequest request, HttpServletResponse response) {
		try {
		super.perform(request, response);
		response.setContentType("text/json");
		
		StmtHelper prep = new StmtHelper();
		Map<String,List<String>> map = prep.db("select price,useCode,finishedSQFT,bathrooms,bedrooms, (select concat(street,' ',city,', ',state,' ',zip))  as ADDRESS,"+
				" ifnull((select image_url from photos where photos.zillowID=alllist.zillowID and photos.photo_num=1),'"+Photo.getNoPhotoURL()+"') IMGURL"+
				" from alllist group by useCode,finishedSQFT,bathrooms,bedrooms,street,city,state,zip,zillowID limit 20").collectStrings.unsafeGet();
		StringBuilder sb = new StringBuilder();
		JSONArray listings = new JSONArray();
		sb.append("<div id=\"homeWorkspaceWrapper\"><div id=\"homeWorkspace\">");
		
		sb.append("<div style=\"height:34px;\" class=\"relative\"><div style=\"top:12px;\" class=\"viewtabs\">");
		sb.append("<div class=\"tab selected\"><a class=\"newlistings\" onclick=\"toggleHomeView(this)\">New Listings</a></div>");
		sb.append("<div class=\"tab\"><a class=\"pricedrops\" onclick=\"toggleHomeView(this)\">Price Drops</a></div>");
		sb.append("<div class=\"tab\"><a class=\"foreclosures\" onclick=\"toggleHomeView(this)\">Foreclosures</a></div>");

		sb.append("</div></div>");
		
		sb.append("<div class=\"wrapScrollable\">");
		
		sb.append("<div id=\"head\"><div class=\"action\"><div class=\"insideAction insideActionLeft\"><a class=\"prev esobutton\" >&#x25C0; Prev</a><a class=\"next esobutton\">Next &#x25BA;</a></div></div>" +
				"<p class=\"textbox\" style=\"z-index: 2;position:relative;text-align:left;float:right;margin: 20px 10px 0px 10px;\"><label class=\"absolutelbl whitelbl\">Zip/Postal Code</label>" +
				"<input type=\"text\" id=\"searchbox\" class=\"esoinput\"  style=\"border:none;display:inline;width:180px;\" name=\"email\" >");
		sb.append("<a onclick=\"var vl = $(this).prev().val();$.address.value('/map/?z='+vl);\" style=\"border:none;\"><img style=\"border:none;position:absolute;padding-left:2px;cursor:pointer;top:4px;height:22px;right:0px;\" src=\""+Initialize.appURL()+"/images/search-icon.jpg\"></a></p>" +
				"<div class=\"tip\" style=\"display:none;z-index:1;position: absolute; right: -20px; top:63px; padding: 0px; \"><div style=\"color: white;height:5px; background:url('/images/icons/leftright.gif') no-repeat;margin-left: 60px;\"></div><div class=\"tipStyle\">click here to start your home search</div></div>" +
				
				"<div class=\"rightpanel\" style=\"height: 332px;box-shadow: #8B8B8B 0px 4px 10px;-webkit-box-shadow: #8B8B8B 0px 4px 10px;display:none;width: 210px;text-align: left;top:60px;background:#5C98DA;position: absolute; right: 0px;\"><div><h5 class=\"label\">Property Type:</h5><br><select name=\"useCode\">" +
		 		"<option value=\"Single Family\">Single Family</option>" +
		 		"<option value=\"Multi Family\">Multi Family</option>" +
		 		"<option value=\"Condo\">Condo</option>" +
		 		"<option value=\"Vacant Land\">Vacant Land</option>" +
		 		"</select></div>" +
		 		"<div id=\"pricefields\">" +
				"<h5 class=\"label\">Price:</h5><br>" +
				"<h6>MIN:&nbsp;</h6><input name=\"minprice\" type=\"text\"><h5 class=\"label\">,000</h5>" +
				"<h6>&nbsp;&nbsp;MAX:&nbsp;</h6><input name=\"maxprice\" type=\"text\"><h5 class=\"label\">,000</h5>" +
				"</div>" +
		 		"<div id=\"chainFields\"><div style=\"float:left;padding-right:16px;\">" +
				"<h5 class=\"label\">Beds:</h5><br><select name=\"bedrooms\"><option value=\"\"></option><option value=\"1\">1+</option>" +
				"<option value=\"2\">2+</option><option value=\"3\">3+</option><option value=\"4\">4+</option><option value=\"5\">5+</option><option value=\"6\">6+</option>" +
				"<option value=\"7\">7+</option><option value=\"8\">8+</option></select></div>" +
				"<div><h5 class=\"label\">Baths:</h5><br><select name=\"bathrooms\"><option value=\"\"></option><option value=\"1\">1+</option>" +
				"<option value=\"2\">2+</option><option value=\"3\">3+</option><option value=\"4\">4+</option>" +
				"<option value=\"5\">5+</option></select></div>" +
		 		"<div id=\"sqft\">" +
		 		"<h5 class=\"label\">Sq. Ft</h5><br><select name=\"finishedSQFT\" style=\"font-size:10px;\"><option value=\"\">Any</option><option value=\"500\">500+</option><option value=\"600\">600+</option><option value=\"700\">700+</option><option value=\"800\">800+</option><option value=\"900\">900+</option><option value=\"1000\">1000+</option><option value=\"1250\">1250+</option><option value=\"1500\">1500+</option><option value=\"2000\">2000+</option><option value=\"3000\">3000+</option><option value=\"5000\">5000+</option><option value=\"10000\">10000+</option></select></div>" +
		 		"<div><button class=\"esobutton\" onclick=\"doPostWithInputs('search.PerformMapSearch','input,select')\" id=\"searchlistings\" title=\"Search Listings\" type=\"submit\">Find Listings</button></div></div></div>" +
		 		
				"</div>");
		sb.append("<div id=\"scrollWrapper\">");
		sb.append(ScrollableListings.out("newlistings"));
		sb.append("</div></div></div>");
		
		sb.append("<div id=\"shortSections\">");
		
		sb.append("<div style=\"text-align:left;\"><div style=\"width:440px;float:left;\">");
		sb.append("<h3 style=\"margin-bottom:5px;text-align:left;\">Daily Email Updates</h3><div class=\"g-section g-tpl-25-75 content\">"+
          "<div class=\"g-unit\" >"+
          "<div>Receive emails about new and updated properties.</div><div>Realtor notifications about changes in the market.</div><div>Create and Save Property Searches.</div> <a id=\"signupnowlink\" extraheight=\"238\" onclick=\"doPost('user.Login');return false;\">Sign In Now</a>." +
          "</div>" +
          "<div class=\"g-unit g-first\"> <img src=\""+Initialize.appURL()+"/images/icons/emailUpdates.jpg\" width=\"100\" height=\"75\"> </div>"+
          "</div></div></div>");
		
		
		sb.append("<div style=\"text-align:left;float:right;\"><div style=\"width:440px;\">");
		sb.append("<h3 style=\"margin-bottom:5px;text-align:left;\">Home Valuation</h3><div class=\"g-section g-tpl-25-75 content\">"+
          "<div class=\"g-unit\" >"+
          "<div>Assess your homes value with exclusive analysis.</div><div>Receive monthly market trend updates.</div><div>Previous home sales, avg market time and more.</div> <a>Start your valuation</a>." +
          "</div>" +
          "<div class=\"g-unit g-first\"> <img src=\""+Initialize.appURL()+"/images/icons/dollarbill.jpg\" width=\"100\" height=\"75\"> </div>"+
          "</div></div></div></div></div>");
		sb.append(javaScriptOut());
		data.put("HOME", sb.toString()).write(response.getWriter());
		} catch (JSONException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public StringBuffer javaScriptOut() {
		StringBuffer sb = new StringBuffer();
		sb.append("<script>");
		sb.append("function toggleHomeView(elem) {\n" +
				"if(!$(elem).parent().hasClass('selected'))\n" +
				"{"+
				"	$('.viewtabs .tab').each(function(){if(!$(this).hasClass('selected') && $(elem).attr('class')==$(this).find(':first-child').attr('class')) $(this).addClass('selected'); else $(this).removeClass('selected'); });\n" +
				"	doPost('home.ScrollableListings',{listingType:$(elem).attr('class')});\n" +
				"}" +
				"}");
		sb.append("$(document).ready(function(){$('#main').data('bubble').addPopUpItems('#signupnowlink');" +
				"$('#searchbox').focus(function(){if($('.rightpanel:hidden').size()>0) {" +
				//"$('#homeWorkspace .wrapScrollable').animate({marginRight:\"210px\"}," +
				"$('.rightpanel').show('slide',{direction:'vertical'},600);" +
				"$('.tip').hide();} });" +
				"" +
				"" +
				"$('p.textbox').hoverIntent({over:function(){if($('.rightpanel:hidden').size()>0)$('.tip').show('fade',250);},out:function(){if($('.rightpanel:hidden').size()>0)$('.tip').hide('fade',140);}}); });");
		sb.append("</script>");
		return sb;
	}

}
